package com.glory.dgcpcpda.activity.mfg;

import android.os.Build;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.DataBinListAdapter;
import com.glory.dgcpcpda.adapter.OfflineBinInfoListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.BinInfoEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: FTOfflineDataReportActivity
 * Author: XJH
 * Date: 2020/8/14 14:06
 * Description:
 * History:
 */
public class FTOfflineDataReportActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvTestEqp)
    TextView tvTestEqp;
    @BindView(R.id.tvStationAb)
    TextView tvStationAb;
    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvCircuit)
    TextView tvCircuit;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvDataName)
    TextView tvDataName;
    @BindView(R.id.rvDataBinList)
    RecyclerView rvDataBinList;
    @BindView(R.id.tvGetData)
    TextView tvGetData;
    @BindView(R.id.tvGrading)
    TextView tvGrading;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.rvBINInfoList)
    RecyclerView rvBINInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private LotInfoEntity lotInfoEntity = new LotInfoEntity();
    private DataBinListAdapter mDataBinListAdapter;
    private OfflineBinInfoListAdapter mBinInfoListAdapter;
    private String mTestEqp = "";
    private List<BinInfoEntity.BinInfo> hbinData2;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_data_report;
    }

    @Override
    protected void initView() {
        initLayout();

        initEdit(etEqpWork, s -> {
            //通过设备获取所有信息
            requestBinForType(s);
        });
    }

    @Override
    protected void initData() {

    }

    private void getLotInfo(String lotid) {
        LotUtils.searchLotInfo(this, lotid, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                lotInfoEntity = response;
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"LOTQUERY");
            }
        });
    }

    public void requestBinForType(String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<BinInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BinInfoEntity>>
                (new TypeToken<BaseResponseBean<BinInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BinInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    getLotInfo(response.body().getResponse().getBody().getLOTID());
                    if (response.body().getResponse().getBody().getHBINLIST().size() <= 0) {
                        tvDataName.setText(response.body().getResponse().getBody().getTESTTYPE());
                        tvEqpType.setText(response.body().getResponse().getBody().getEQPTYPE());
                        tvTestEqp.setText(response.body().getResponse().getBody().getTESTERIDS());
                        tvStationAb.setText(response.body().getResponse().getBody().getSTATIONAB());
                        tvLot.setText(response.body().getResponse().getBody().getLOTID());
                        tvCircuit.setText(response.body().getResponse().getBody().getPRODUCTMODEL());
                        tvNum.setText(response.body().getResponse().getBody().getLOTCOUNT());
                        mTestEqp = response.body().getResponse().getBody().getTESTERIDS();
                        tvGrading.setText(response.body().getResponse().getBody().getGRADINGMARKER());
                        return;
                    }

                    rvDataBinList.setVisibility(View.VISIBLE);
                    mDataBinListAdapter.setNewData(response.body().getResponse().getBody().getHBINLIST());
                    tvDataName.setText(response.body().getResponse().getBody().getTESTTYPE());
                    tvEqpType.setText(response.body().getResponse().getBody().getEQPTYPE());
                    tvLot.setText(response.body().getResponse().getBody().getLOTID());
                    tvCircuit.setText(response.body().getResponse().getBody().getPRODUCTMODEL());
                    tvNum.setText(response.body().getResponse().getBody().getLOTCOUNT());
                    mTestEqp = response.body().getResponse().getBody().getTESTERIDS();
                    tvGrading.setText(response.body().getResponse().getBody().getGRADINGMARKER());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BinInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "BININFOREQUEST");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "Report");
        map.addProperty("EQUIPMENTID", eqpID);
        map.addProperty("LOTID", "");
        map.addProperty("TESTTYPE", "");
        NetUtils.requestPostNet("RTM.BININFOREQUEST", "BININFOREQUEST", this, map, callBack);
    }

    private void initLayout() {
        listTv1.setText("BIN名称");
        listTv2.setText("BIN描述");
        listTv3.setText("数量");
        listTv4.setVisibility(View.GONE);
        tvTitle.setText("FT离线上报");
        setMainLayout(llMain);
        rvDataBinList.setLayoutManager(new GridLayoutManager(this, 2));
        rvDataBinList.setItemAnimator(new DefaultItemAnimator());
        mDataBinListAdapter = new DataBinListAdapter(null, false);
        mDataBinListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mDataBinListAdapter.isFirstOnly(true);
        rvDataBinList.setAdapter(mDataBinListAdapter);
        initRecyclerView(rvBINInfoList, this);
        mBinInfoListAdapter = new OfflineBinInfoListAdapter(null, false);
        mBinInfoListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mBinInfoListAdapter.isFirstOnly(true);
        rvBINInfoList.setAdapter(mBinInfoListAdapter);

    }

    @OnClick({R.id.tvBaseSubmit, R.id.tvGetData})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (actionControl("Submit05")) {
                    return;
                }
                if (getText(etEqpWork).equals("")) {
                    showToast("设备信息不能为空", false);
                    return;
                }

                if (mBinInfoListAdapter.getData().size() <= 0){
                    showToast("请先获取数据",false);
                    return;
                }

                List<BinInfoEntity.BinInfo> data = mBinInfoListAdapter.getData();
                int sum = 0 ;
                for (BinInfoEntity.BinInfo info : data){
                    sum += info.getBINQTY();
                }

                if (sum <= 0){
                    showToast("BIN总数量为0，无法提交",false);
                    return;
                }
                if (!data.containsAll(hbinData2)) {
                    data.addAll(hbinData2);
                }
                LotUtils.binReport(this, getSupportFragmentManager(), "Report", tvDataName.getText().toString(),
                        tvLot.getText().toString(), getText(etEqpWork), lotInfoEntity, data,tvGrading.getText().toString(),tvEqpType.getText().toString(),mTestEqp,"","TESTREPORTSUBMIT","OFFLINE",false);
                break;
            case R.id.tvGetData:
                if (getText(etEqpWork).equals("")) {
                    showToast("请先扫描设备信息", false);
                    return;
                }

                requestBinInfo();
                break;
        }

    }


    private void requestBinInfo() {
        FreeUI_EntityCallBack<BaseResponseBean<BinInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BinInfoEntity>>
                (new TypeToken<BaseResponseBean<BinInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BinInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if (response.body().getResponse().getBody().getHBINLIST().size() <= 0) {
                        showConfirmDialog("暂无数据");
                        return;
                    }
                    List<BinInfoEntity.BinInfo> hbinData = new ArrayList<>();
                    hbinData2 = new ArrayList<>();
                    List<BinInfoEntity.BinInfo> hbinlist = response.body().getResponse().getBody().getHBINLIST();
                    for (BinInfoEntity.BinInfo data : hbinlist){
                        if (data.getBINNO().contains("BIN")){
                            hbinData.add(data);
                        }else {
                            hbinData2.add(data);
                        }
                    }
//                    BinInfoEntity.BinInfo binInfo9 = new BinInfoEntity.BinInfo("BIN9", "少数(虚拟BIN)");
//                    BinInfoEntity.BinInfo binInfo10 = new BinInfoEntity.BinInfo("BIN10", "报废(虚拟BIN)");
//                    BinInfoEntity.BinInfo binInfo11 = new BinInfoEntity.BinInfo("BIN11", "样品");
//                    BinInfoEntity.BinInfo binInfo12 = new BinInfoEntity.BinInfo("BIN12", "未测BIN");
//                    BinInfoEntity.BinInfo binInfo13 = new BinInfoEntity.BinInfo("BIN13", "掉料(虚拟BIN)");
//                    hbinData.add(binInfo9);
//                    hbinData.add(binInfo10);
//                    hbinData.add(binInfo11);
//                    hbinData.add(binInfo12);
//                    hbinData.add(binInfo13);
                    mBinInfoListAdapter.setNewData(hbinData);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BinInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EAP.GETHBINREQUEST");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EQPID", getText(etEqpWork));
        map.addProperty("CONNECTIONSTATE", "OFFLINE");
        NetUtils.requestPostNet("EAP.GETHBINREQUEST", "GETHBINREQUEST", this, map, callBack);

    }
}
